package com.zhny.aiot.gdsfj.plugin.model;

import com.zhny.aiot.plugin.core.model.IRequestMessage;
import com.zhny.aiot.plugin.core.utils.CRCUtils;
import com.zhny.aiot.plugin.modbus.handler.model.IModemRequestMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * @Author pengshuai
 * @Date 2024/7/10
 */
public class ModbusFloatRequestMessage  implements IRequestMessage, IModemRequestMessage {
    private int dataAddress;
    private  short func;
    private  int regAddress;
    private Float dataCount;

    public ModbusFloatRequestMessage(int dataAddress,short func,int regAddress,Float dataCount){
        this.dataAddress = dataAddress;
        this.func = func;
        this.regAddress = regAddress;
        this.dataCount = dataCount;
    }

    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        builder.append("dataAddress:").append(dataAddress);
        builder.append("func:").append(func);
        builder.append("regAddress:").append(regAddress);
        builder.append("dataCount:").append(dataCount);
        builder.append("}");
        return builder.toString();
    }
    public int getDataAddress() {
        return dataAddress;
    }

    public Float getDataCount() {
        return dataCount;
    }

    public short getFunc() {
        return func;
    }

    public int getRegAddress() {
        return regAddress;
    }
    @Override
    public String getKey() {
        return String.valueOf(dataAddress);
    }

    @Override
    public ByteBuf enCoder() {
        ByteBuf byteBuf = Unpooled.buffer();
        byteBuf.writeByte(this.getDataAddress ());
        byteBuf.writeByte(this.getFunc ());
        byteBuf.writeShort(this.getRegAddress ());
        byteBuf.writeFloat(this.getDataCount());
        int crc = CRCUtils.calculateCRC(byteBuf, byteBuf.readerIndex(), 8);
        byteBuf.writeShort(crc);
        return byteBuf;
    }
}
